<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
class FieldDefinition
{

  protected $params;

  /**
   *
   * @var string
   */
  protected $table;

  /**
   *
   * @var boolean
   */
  protected $primaryKey;

  /**
   *
   * @var string
   */
  protected $name;

  /**
   * text
   * boolean
   * integer, decimal, float
   * date, time, timestamp
   *
   * @var string
   */
  protected $type;

  /**
   *
   * @var string
   */
  protected $sql;

  /**
   *
   * @var boolean
   */
  protected $autoIncrement;

  /**
   *
   * @var int
   */
  protected $length;

  /**
   *
   * @var boolean
   */
  protected $unsigned;

  /**
   *
   * @var mixed
   */
  protected $default;

  /**
   *
   * @var boolean
   */
  protected $notNull;

  /**
   * Constructor.
   *
   * @param array|IDictionary $def
   * @return void
   */
  public function __construct($name, $type, $params = null)
  {
    $this->setName($name);
    $this->setType($type);

    if (! empty($params)) {
      $params = Dictionary::parseFrom($params);

      $this->setSql($params->get('sql'));
      $this->setLength($params->get('length'));
      $this->setPrimaryKey($params->get('primary_key'));
      $this->setAutoIncrement($params->get('auto_increment'));
      $this->setNotNull($params->get('not_null'));
      $this->setUnsigned($params->get('unsigned'));
      $this->setDefault($params->get('default'));

      $this->setTable($params->get('table'));
    }
  }

  public function getDefinition()
  {
    return array(
      $this->getName(),
      $this->getType(),
      array(
        'sql' => $this->getSql(),
        'length' => $this->getLength(),
        'primary_key' => $this->getPrimaryKey(),
        'auto_increment' => $this->getAutoIncrement(),
        'unsigned' => $this->getUnsigned(),
        'not_null' => $this->getNotNull(),
        'default' => $this->getDefault(),

        'table' => $this->getTable()
      )
    );
  }

  /**
   * @param $sql the $sql to set
   */
  public function setTable($table)
  {
    $this->table = $table;
  }

  /**
   * @return the $sql
   */
  public function getTable()
  {
    return $this->table;
  }

  /**
   * @param $sql the $sql to set
   */
  public function setSql($sql)
  {
    $this->sql = $sql;
  }

  /**
   * @return the $sql
   */
  public function getSql()
  {
    return $this->sql;
  }

  /**
   * @return the $name
   */
  public function getName()
  {
    return $this->name;
  }

  /**
   * @param $primaryKey the $primaryKey to set
   */
  public function setPrimaryKey($primaryKey)
  {
    $this->primaryKey = $primaryKey;
  }

  /**
   * @return the $primaryKey
   */
  public function getPrimaryKey()
  {
    return $this->primaryKey;
  }

  /**
   * @return the $type
   */
  public function getType()
  {
    return $this->type;
  }

  /**
   * @return the $autoIncrement
   */
  public function getAutoIncrement()
  {
    return $this->autoIncrement;
  }

  /**
   * @return the $length
   */
  public function getLength()
  {
    return $this->length;
  }

  /**
   * @return the $unsigned
   */
  public function getUnsigned()
  {
    return $this->unsigned;
  }

  /**
   * @return the $notNull
   */
  public function getNotNull()
  {
    return $this->notNull;
  }

  /**
   * @return the $default
   */
  public function getDefault()
  {
    return $this->default;
  }

  /**
   * @param $name the $name to set
   */
  public function setName($name)
  {
    $this->name = $name;
  }

  /**
   * @param $type the $type to set
   */
  public function setType($type)
  {
    $this->type = $type;
  }

  /**
   * @param $autoIncrement the $autoIncrement to set
   */
  public function setAutoIncrement($autoIncrement)
  {
    $this->autoIncrement = $autoIncrement;
  }

  /**
   * @param $length the $length to set
   */
  public function setLength($length)
  {
    $this->length = $length;
  }

  /**
   * @param $unsigned the $unsigned to set
   */
  public function setUnsigned($unsigned)
  {
    $this->unsigned = $unsigned;
  }

  /**
   * @param $notNull the $notNull to set
   */
  public function setNotNull($notNull)
  {
    $this->notNull = $notNull;
  }

  /**
   * @param $default the $default to set
   */
  public function setDefault($default)
  {
    $this->default = $default;
  }

}

?>